<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Current_feedback extends CI_Controller {
	public function index($refresh = false)
	{
            $this->load->model('feedbacks');
            $feedback = $this->feedbacks->getAllNew();
            
            $data['title'] = "Current Feedback";
            $data['data'] = array (
                "feedback"=>$feedback,
                "success"=> $this->session->flashdata('feedback_response'),
                );

            $data['view'] = "feedback/view_current_feedback";

            $data['css'] = array("table.css");
            $data['js'] = array("feedback.js");
            
            $data['refresh'] = $refresh;

            $this->load->view('view_template',$data);
	}
        
        public function viewFeedback(){
//            extract($this->ajax->post("feedback_id"));
            $feedback_id = $this->input->get("feedback_id");
            $this->load->model('feedbacks');
            $feedback = $this->feedbacks->getFeedback($feedback_id);
            
            $this->load->view("ajax/view_feedback_response", array("feedback" => $feedback));
//            $data['view'] = "feedback/view_current_feedback";
//            $data['feedback'] = $feedback;
//            $this->load->view('view_basic_template',$data);
        }
        
        public function submitFeedback(){
            extract($this->ajax->post("feedback_id","feedback_response"));
            if ($feedback_id == "" || $feedback_response == ""){
                redirect('/feedback/current_feedback/', 'refresh');
                return;
            }
            $this->load->model("feedbacks");
            
            $staffuser_id = $this->session->userdata('user_id');
            $this->feedbacks->respond($feedback_id, filter_profanity($feedback_response), $staffuser_id);
            
            
            $this->sendFeedback($feedback_id);
            
            $this->session->keep_flashdata('feedback_response', 'Response was successful.');
            $this->index(true);
        }
        
        public function discardFeedback(){
            $feedback_id = $this->input->post("feedback_id");
            $this->load->model("feedbacks");
            if ($this->feedbacks->discardFeedback($feedback_id)){
                $this->session->set_flashdata('feedback_response', 'Feedback deleted successfully.');
                echo "good";
            }
            else{
                $this->session->set_flashdata('feedback_response', 'Feedback discard failed. No changes have been made.');
                echo "fail";
            }
        }
        
        private function sendFeedback($feedback_id){
            
            $this->load->model('feedbacks');
            $this->load->model('users');
            $feedback = $this->feedbacks->getFeedback($feedback_id);
            $user = $this->users->getByID($feedback->user_id);
            
            $date = date("l jS \of F Y", strtotime($feedback->feedback_date));
            
            //Send email
            $to      = $user->user_email;
            $subject = 'Response to Feedback left on '.$date.'. Do not reply to this email.';
            $message = "You have recieved a response from feedback that you left on the $date.\r\n".
                       "Response:\r\n $feedback->feedback_response\r\n\r\n\r\n".
                       "Original Comment:\r\n $feedback->feedback_comment\r\n\r\n".
                       "Thanks,\r\nRMPlanner";
            
               send_email_advanced($to, $subject, $message);
            
            //Send notification
            $url = 'http://54.245.110.54/SendNotification.php';

            //what post fields?
            $fields = array(
                'msg'=>'Response to feedback left on '.$date.': '.$feedback->feedback_response,
                'user'=>$feedback->user_id
            );

            //build the urlencoded data
            $postvars='';
            $sep='';
            foreach($fields as $key=>$value){
                $postvars.= $sep.urlencode($key).'='.urlencode($value); 
                $sep='&'; 
            }
            //open connection
            $ch = curl_init();

            //set the url, number of POST vars, POST data
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_POST,count($fields));
            curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
            
            //execute post
            $result = curl_exec($ch);
            
            //close connection
            curl_close($ch);
        }
}